﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebApp.ViewModels
{
    public class PageInfoModel
    {
        public int pageIndex { get; set; }
        public int pageSize { get; set; }
        public int pages { get; set; }
        public int totalRows { get; set; }
        public List<PageItem> pageList { get; set; }

        public PageInfoModel(int _page, int _pagesize, int _totalrows)
        {
            this.pageIndex = _page;
            this.pageSize = _pagesize;
            this.totalRows = _totalrows;
            this.pages = (int)Math.Ceiling((float)_totalrows / _pagesize);
            this.pageList = new List<ViewModels.PageItem>();

            if (this.pages <= 0)
            {
                this.pageList.Add(new ViewModels.PageItem(1));
            }
            else if (this.pages <= 9)
            {
                for (var i = 1; i <= this.pages; i++)
                {
                    this.pageList.Add(new ViewModels.PageItem(i));
                }
            }
            else
            {
                if (this.pageIndex < 6)
                {
                    for (var i = 1; i <= 7; i++)
                    {
                        this.pageList.Add(new ViewModels.PageItem(i));
                    }
                    this.pageList.Add(new ViewModels.PageItem(8, "..."));
                    this.pageList.Add(new ViewModels.PageItem(this.pages));
                }
                else if (this.pageIndex > this.pages - 5)
                {
                    this.pageList.Add(new ViewModels.PageItem(1));
                    this.pageList.Add(new ViewModels.PageItem(this.pageIndex - 7, "..."));
                    for (var i = this.pages - 6; i <= this.pages; i++)
                    {
                        this.pageList.Add(new ViewModels.PageItem(i));
                    }
                }
                else
                {
                    this.pageList.Add(new ViewModels.PageItem(1));
                    this.pageList.Add(new ViewModels.PageItem(this.pageIndex - 3, "..."));
                    for (var i = this.pageIndex - 2; i <= this.pageIndex + 2; i++)
                    {
                        this.pageList.Add(new ViewModels.PageItem(i));
                    }
                    this.pageList.Add(new ViewModels.PageItem(this.pageIndex + 3, "..."));
                    this.pageList.Add(new ViewModels.PageItem(this.pages));
                }
            }
        }
    }

    public class PageItem
    {
        public int page { get; set; }
        public string name { get; set; }
        public PageItem(int page)
        {
            this.page = page;
            this.name = page.ToString();
        }
        public PageItem(int page, string name)
        {
            this.page = page;
            this.name = name;
        }
    }
}
